package com.xxxt.wx;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.xxxt.dao.PracticeMapper;
import com.xxxt.dao.StudentMapper;
import com.xxxt.dao.TeacherMapper;
import com.xxxt.entity.Comment;
import com.xxxt.entity.Company;
import com.xxxt.entity.Practice;
import com.xxxt.entity.Student;
import com.xxxt.entity.Teacher;
import com.xxxt.jxj.service.CommentService;
import com.xxxt.utils.DateUtil;
import com.xxxt.utils.FileUploadUtil;
import com.xxxt.utils.MD5Util;
import com.xxxt.utils.MailUtil;
import com.xxxt.utils.PropertyCheckUtil;
import com.xxxt.utils.UUIDUtil;
import com.xxxt.wsc.service.PracticeService;
import com.xxxt.zpc.service.CommonService;
import com.xxxt.zpc.service.CompanyService;
import com.xxxt.zpc.service.InformService;

/**
 * @description:微信端的需要写入/修改数据库的接口
 * @author:wsc
 * @date:2020年6月10日 下午11:18:13
 */
@RestController
@RequestMapping("/wx/")
public class WechatInputController {

	@Autowired
	PracticeService practiceService;

	@Autowired
	private TeacherMapper teacherMapper;

	@Autowired
	private StudentMapper studentMapper;

	@Autowired
	private CommonService commonService;

	@Autowired
	CompanyService companyService;

	@Autowired
	private CommentService commentService;

	@Autowired
	PracticeMapper practiceMapper;

	@Autowired
	private InformService informService;

	/*
	 * 修改密码，所有用户修改密码入口
	 */
	@PostMapping(value = "updatePassword")
	public JSONObject updatePassword(@RequestBody Map<String, String> map) {
		JSONObject json = new JSONObject();
		// 状态码
		int status = -1;
		// 提示信息
		String message = "修改密码失败，请重试！";

		String identity = map.get("identity");
		String id = map.get("account");
		String password = map.get("password");
		String newpassword = map.get("newpassword");
		try {
			password=MD5Util.md5withkey(password, "xxxt");
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if (commonService.selectStudentByIdAndPassword(identity, id, password) > 0) {
			if (newpassword.length() < 6) {
				message = "密码长度不能少于6";
				status = 1;
				json.put("status", status);
				json.put("message", message);
				return json;
			}
			try {
				newpassword = MD5Util.md5withkey(newpassword, "xxxt");
			} catch (Exception e) {
				e.printStackTrace();
				json.put("status", status);
				json.put("message", message);
				return json;
			}
			Boolean result = commonService.updatePasswordById(identity, newpassword, id);
			if (result) {
				status = 0;
				message = "修改成功！";
			}
			json.put("status", status);
			json.put("message", message);
			return json;
		} else {
			status = -1;
			message = "原密码错误";
			json.put("status", status);
			json.put("message", message);
			return json;
		}

	}

	// 上传图片
	@PostMapping("uploadPhoto")
	public JSONObject uploadPhoto(MultipartFile file, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		JSONObject json = new JSONObject();
		// 状态码
		int status = -1;
		// 提示信息
		String message = "";
		// 上传文件路径
		String filePath = "";
		// 获取学号
		String sno = (String) request.getSession().getAttribute("account");
		if (sno == null) {
			sno = request.getParameter("sno");
		}
		// 判断上传文件是否为图片
		else if (file == null && !FileUploadUtil.CheckImages(file)) {
			message = "请上传正确的图片格式";
			status = 1;
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		try {
			if (message.length() == 0) {
				filePath = practiceService.uploadTestify(request, response, file, sno);
				status = 0;
				message = "上传成功";
				json.put("filePath", filePath);
				json.put("status", status);
				json.put("message", message);
				return json;
			}
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			message = "上传失败";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		return json;
	}

	// 删除临时文件
	@PostMapping("deletePhoto")
	public JSONObject deletePhoto(@RequestBody Map<String, String> map, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		JSONObject json = new JSONObject();
		// 状态码
		int status = -1;
		// 提示信息
		String message = "";
		String filePath = map.get("filePath");
		if (filePath != null && !"".equals(filePath)) {
			try {
				practiceService.deleteTestify(filePath);
			} catch (Exception e) {
				e.printStackTrace();
				status = -1;
				message = "删除失败";
				json.put("status", status);
				json.put("message", message);
				return json;
			}
		} else {
			status = 1;
			message = "删除路径为空";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		status = 0;
		message = "删除成功";
		json.put("status", status);
		json.put("message", message);
		return json;
	}

	/**
	 * 
	 * @description：添加或修改实习记录
	 * @param entryTime    入职时间
	 * @param companyId    实习公司Id
	 * @param job          岗位
	 * @param leaderName   直属领导
	 * @param leaderPhone  领导电话
	 * @param entryTestify 入职材料
	 * @param request
	 * @param response
	 * @throws IOException @return：
	 */
	@SuppressWarnings({ "unchecked" })
	@PostMapping("addEntryRecord")
	public JSONObject addPractice(@RequestBody Map<String, Object> map, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws IOException {
		JSONObject json = new JSONObject();
		int status = 1;
		String message = "";
		// 获取请求数据
		String companyId = (String) map.get("companyId");
		String entryTime = (String) map.get("entryTime");
		String job = (String) map.get("job");
		String leaderName = (String) map.get("leaderName");
		String leaderPhone = (String) map.get("leaderPhone");
		String sno = (String) map.get("sno");
		List<Map<String, String>> entryTestifys = (List<Map<String, String>>) map.get("entryTestify");
		List<String> entryTestify = new ArrayList<String>();
		// String entryTime = request.getParameter("entryTime");
		// String companyId = request.getParameter("companyId");
		// String job = request.getParameter("job");
		// String leaderName = request.getParameter("leaderName");
		// String leaderPhone = request.getParameter("leaderPhone");
		// String sno = request.getParameter("sno");
		if (entryTime == null) {
			message = "时间不能为空";
		} else if (companyId == null) {
			message = "公司不能为空";
		} else if (job == null || "".equals(job)) {
			message = "岗位不能为空";
		} else if (leaderName == null || "".equals(leaderName)) {
			message = "领导姓名不能为空";
		} else if (leaderPhone == null || "".equals(leaderPhone)) {
			message = "电话格式不正确";
		} else if (entryTestifys == null || entryTestifys.size() == 0) {
			message = "证明材料不能为空";
		} else if (PropertyCheckUtil.isPhone(leaderPhone) == false) {
			message = "手机格式不正确";
		}
		try {
			if (message.length() == 0) {
				job = job.replaceAll(" ", "");
				leaderName = leaderName.replaceAll(" ", "");
				leaderPhone = leaderPhone.replaceAll(" ", "");
				// 获取该生的实习记录
				List<Practice> list = practiceMapper.queryPracticeListBySno(sno, null, null);
				// 判断是否还处于就职状态
				if (list.size() > 0) {
					Practice p = list.get(0);
					if (p.getLeaveTime() != null && !"".equals(p.getLeaveTime())) {
						if (!(Integer.parseInt(entryTime.replaceAll("-", "")) > Integer
								.parseInt(p.getLeaveTime().replaceAll("-", ""))
								|| Integer.parseInt(entryTime.replaceAll("-", "")) < Integer
										.parseInt(p.getEntryTime().replaceAll("-", "")))) {
							status = 4;
							message = "该时间段您还处于就职阶段，请选择其他日期！";
							json.put("status", status);
							json.put("message", message);
							return json;
						}
					} else {
						status = 2;
						message = "您还处于就职状态，不能提交新的入职登记！";
						json.put("status", status);
						json.put("message", message);
						return json;
					}
					if (entryTestifys.size() > 0) {
						for (Map<String, String> m : entryTestifys) {
							entryTestify.add((String) m.get("url"));
						}
					}
					boolean b = practiceService.addPractice(entryTime, companyId, sno, job, leaderName, leaderPhone,
							entryTestify);
					if (b) {
						status = 0;
						message = "添加成功";
						json.put("status", status);
						json.put("message", message);
						return json;
					}
				} else {
					if (entryTestifys.size() > 0) {
						for (Map<String, String> m : entryTestifys) {
							String string = m.get("url");
							entryTestify.add(string);
						}
					}
					boolean b = practiceService.addPractice(entryTime, companyId, sno, job, leaderName, leaderPhone,
							entryTestify);
					if (b) {
						status = 0;
						message = "添加成功";
						json.put("status", status);
						json.put("message", message);
						return json;
					}
				}
			} else {
				json.put("status", status);
				json.put("message", message);
				return json;
			}
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			message = "操作失败";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		return json;
	}

	@SuppressWarnings("unchecked")
	@PostMapping("updateEntryRecord")
	public JSONObject updatePractice(@RequestBody Map<String, Object> map, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws IOException, ParseException {
		JSONObject json = new JSONObject();
		int status = 1;
		String message = "";
		// 获取请求数据
		String practiceId = (String) map.get("practiceId");
		String entryTime = (String) map.get("entryTime");
		String job = (String) map.get("job");
		String leaderName = (String) map.get("leaderName");
		String leaderPhone = (String) map.get("leaderPhone");
		String sno = (String) map.get("sno");
		List<Map<String, String>> entryTestifys = (List<Map<String, String>>) map.get("entryTestify");
		List<String> entryTestify = new ArrayList<String>();
		// String entryTime = request.getParameter("entryTime");
		// String companyId = request.getParameter("companyId");
		// String job = request.getParameter("job");
		// String leaderName = request.getParameter("leaderName");
		// String leaderPhone = request.getParameter("leaderPhone");
		// String sno = request.getParameter("sno");
		if (entryTime == null) {
			message = "时间不能为空";
		} else if (job == null || "".equals(job)) {
			message = "岗位不能为空";
		} else if (leaderName == null || "".equals(leaderName)) {
			message = "领导姓名不能为空";
		} else if (leaderPhone == null || "".equals(leaderPhone)) {
			message = "领导电话不能为空";
		} else if (entryTestifys == null || entryTestifys.size() == 0) {
			message = "证明材料不能为空";
		}
		if (message.length() == 0) {
			try {
				job = job.replaceAll(" ", "");
				leaderName = leaderName.replaceAll(" ", "");
				leaderPhone = leaderPhone.replaceAll(" ", "");
				// 获取该生的实习记录
				List<Practice> list = practiceMapper.queryPracticeListBySno(sno, null, null);
				if (practiceId != null && !"".equals(practiceId) && !"null".equals(practiceId)) {
					// 判断所选的时间是否处于就职状态
					if (list.size() > 0) {
						for (Practice p : list) {
							if (p.getPracticeId().equals(practiceId)) {
								continue;
							}
							if (p.getLeaveTime() != null && !"".equals(p.getLeaveTime())) {
								if (!(Integer.parseInt(entryTime.replaceAll("-", "")) > Integer
										.parseInt(p.getLeaveTime().replaceAll("-", ""))
										|| Integer.parseInt(entryTime.replaceAll("-", "")) < Integer
												.parseInt(p.getEntryTime().replaceAll("-", "")))) {
									status = 4;
									message = "该时间段您还处于就职阶段，请选择其他日期！";
									json.put("status", status);
									json.put("message", message);
									return json;
								}
							} else {
								if (!DateUtil.isDateCompare2(entryTime, p.getEntryTime())) {
									status = 4;
									message = "该时间段您还处于就职阶段，请选择其他日期！";
									json.put("status", status);
									json.put("message", message);
									return json;
								}
							}
						}
						if (entryTestifys.size() > 0) {
							for (Map<String, String> m : entryTestifys) {
								entryTestify.add((String) m.get("url"));
							}
						}
						boolean b = practiceService.updateEntryPractice(practiceId, entryTime, job, leaderName,
								leaderPhone, entryTestify);
						if (b) {
							status = 0;
							message = "修改成功";
							json.put("status", status);
							json.put("message", message);
							return json;
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				status = -1;
				message = "修改失败";
				json.put("status", status);
				json.put("message", message);
				return json;
			}
		}
		return json;

	}

	/**
	 * 
	 * @author: wsc
	 * @date:2020年6月17日 上午11:14:38 @description：
	 * @param leaveTestify 离职材料
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 * @throws IOException：
	 */
	@SuppressWarnings({ "unchecked" })
	@PostMapping("updateLeaveRecord")
	public JSONObject updateLeaveRecord(@RequestBody Map<String, Object> map, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws IOException {
		JSONObject json = new JSONObject();
		int status = 1;
		String message = "";
		String practiceId = (String) map.get("practiceId");
		String leaveTime = (String) map.get("leaveTime");
		String leaveReason = (String) map.get("leaveReason");
		List<Map<String, Object>> leaveTestifys = (List<Map<String, Object>>) map.get("leaveTestify");
		List<String> leaveTestify = new ArrayList<String>();
		String sno = (String) map.get("sno");
		// String practiceId = request.getParameter("practiceId");
		// String leaveTime = request.getParameter("leaveTime");
		// String leaveReason = request.getParameter("leaveReason");
		// String leaveTestify = request.getParameter("leaveTestify");
		if (leaveTime == null || "".equals(leaveTime)) {
			message = "时间不能为空";
		} else if (leaveReason == null || "".equals(leaveReason)) {
			message = "离职原因不能为空";
		} else if (leaveTestifys == null || leaveTestifys.size() == 0) {
			message = "未选择离职证明材料";
		}
		// 获取该生的实习记录
		Practice practice = practiceMapper.queryPracticeById(practiceId);
		if (practice == null) {
			status = 4;
			message = "该实习记录已不存在，请先填写入职登记";
		}
		if (message.length() == 0) {

			leaveReason = leaveReason.replaceAll(" ", "");

			List<Practice> list = practiceMapper.queryPracticeListBySno(sno, null, null);// 判断所选的时间是否处于就职状态
			boolean b;
			try {
				if (list.size() > 0) {
					for (Practice p : list) {
						if (p.getPracticeId().equals(practiceId)) {
							if (DateUtil.isDateCompare2(leaveTime, p.getEntryTime())) {
								status = 3;
								message = "离职时间要大于入职时间";
								json.put("status", status);
								json.put("message", message);
								return json;
							} else {
								continue;
							}
						}
						if (p.getLeaveTime() != null && !"".equals(p.getLeaveTime())) {
							if (!(Integer.parseInt(leaveTime.replaceAll("-", "")) > Integer
									.parseInt(p.getLeaveTime().replaceAll("-", ""))
									|| Integer.parseInt(leaveTime.replaceAll("-", "")) < Integer
											.parseInt(p.getEntryTime().replaceAll("-", "")))) {
								status = 4;
								message = "该时间段您还处于就职阶段，请选择其他日期！";
								json.put("status", status);
								json.put("message", message);
								return json;
							}
						} else {
							if (!DateUtil.isDateCompare2(leaveTime, p.getEntryTime())) {
								status = 4;
								message = "该时间段您还处于就职阶段，请选择其他日期！";
								json.put("status", status);
								json.put("message", message);
								return json;
							}
						}
					}
				}
				if (leaveTestifys.size() > 0) {
					for (Map<String, Object> m : leaveTestifys) {
						leaveTestify.add((String) m.get("url"));
					}
				}
				b = practiceService.updateLeavePractice(practiceId, leaveTime, leaveReason, leaveTestify, request,
						response);
				if (b) {
					status = 0;
					message = "修改成功";
					json.put("status", status);
					json.put("message", message);
					return json;
				}
			} catch (Exception e) {
				e.printStackTrace();
				status = -1;
				message = "操作失败";
				json.put("status", status);
				json.put("message", message);
				return json;
			}
			return json;
		} else {
			json.put("status", status);
			json.put("message", message);
			return json;
		}
	}

	// 删除实习记录
	@GetMapping("deletePractice")
	public JSONObject deletePractice(HttpServletRequest request, String practiceId) {
		JSONObject json = new JSONObject();
		int status = 0;
		String message = "删除成功";
		boolean b = false;
		try {
			b = practiceService.deletePractice(request, practiceId);
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			message = "删除失败";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		if (b) {
			json.put("status", status);
			json.put("message", message);
		} else {
			status = 1;
			message = "删除失败";
			json.put("status", status);
			json.put("message", message);
		}
		return json;
	}

	/**
	 * @describe 学生修改个人信息
	 * @param sno           学生学号
	 * @param phone         学生手机号
	 * @param email         学生邮箱
	 * @param idcard        学生身份证
	 * @param liveSituation 学生住宿情况
	 * @param birthPlace    学生籍贯
	 * @return
	 */
	@RequestMapping(value = "modifyStudentInfo", method = RequestMethod.GET)
	public JSONObject modifyStudentInfo(HttpServletRequest request) {
		JSONObject json = new JSONObject();

		// 数据处理
			String name;
			String sno;
			String phone;
			String sex;
			String email;
			String liveSituation;
			String birthPlace;
			name = request.getParameter("name");
			sno = request.getParameter("sno");
			sex = request.getParameter("sex");
			email = request.getParameter("email");
			liveSituation = request.getParameter("liveSituation");
			birthPlace = request.getParameter("birthPlace");
		
		if (name == null || "".equals(name) || sno == null || "".equals(sno)
					|| sex == null || "".equals(sex) || email == null || "".equals(email) || liveSituation == null || "".equals(liveSituation) || birthPlace == null
					|| "".equals(birthPlace)) {
				json.put("status", 1);
				json.put("message", "传入字段不合法！");
				return json;
			}
			birthPlace = birthPlace.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\"", "");
		// 数据校验：
		// 校验邮箱
		boolean checkemail = MailUtil.checkemail(email);
		if (checkemail == false) {
			json.put("status", 5);
			json.put("message", "邮箱格式不正确！");
			return json;
		}
		// 校验姓名
		boolean name2 = MailUtil.checkname(name);
		if (name2 == false) {
			json.put("status", 6);
			json.put("message", "姓名格式不正确！");
			return json;
		}
		// 校验学生是否存在
		Student student = new Student(sno, sex, email, liveSituation, birthPlace);
		student.setName(name);
		Student queryOneStudentBySno;
		try {
			queryOneStudentBySno = studentMapper.queryOneStudentBySno(sno);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "服务器内部错误");
			return json;
		}
		if (queryOneStudentBySno == null) {
			json.put("status", 2);
			json.put("message", "修改信息失败，学生不存在！");
			return json;
		}
		try {
			// 进行修改
			studentMapper.updateStudentBySno(student);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "服务器内部错误");
			return json;
		}
		json.put("status", 0);
		json.put("message", "修改成功！");
		return json;
	}

	/**
	 * @describe 教师注册账号
	 * @param tno           教师输入工号
	 * @param classGradeId  教师选择班级id
	 * @param password      教师注册输入注册密码
	 * @param againPassword 教师再次输入注册密码
	 * @param phone         教师手机号
	 * @param email         教师邮箱
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "teacherRegister", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject teacherRegister(HttpSession session, String tno, String name, String password,
			String againPassword, String email, String checkCode) throws Exception {
		JSONObject json = new JSONObject();
		// 判断字段合法性
		if (tno == null || "".equals(tno) || name == null || "".equals(name) || password == null || "".equals(password)
				|| againPassword == null || "".equals(againPassword) || email == null || "".equals(email)
				|| checkCode == null || "".equals(checkCode)) {
			json.put("status", 1);
			json.put("message", "传入数据不合法!");
			return json;
		}
		// 校验名字正确性
		boolean checkname = MailUtil.checkname(name);
		if (checkname == false) {
			json.put("status", 9);
			json.put("message", "名字格式不合法!");
			return json;
		}
		boolean checkemail = MailUtil.checkemail(email);
		if (checkemail == false) {
			json.put("status", 10);
			json.put("message", "邮箱格式不合法!");
			return json;
		}
		String md5withkey = MD5Util.md5withkey(password, "xxxt");
		String registerStatus = "waiting";
		// 判断密码一致性
		if (!password.equals(againPassword)) {
			json.put("status", 2);
			json.put("message", "两次输入密码不一致!");
			return json;
		}
		// 查询验证码
		String sessionCheckCode = (String) session.getAttribute("checkCode");
		if (!checkCode.equals(sessionCheckCode)) {
			json.put("status", 6);
			json.put("message", "验证码不正确，请重新输入!");
			return json;
		}
//		//查询班级是否存在
//		ClassGrade selectClassGradeByClassGradeId = classGradeMapper.selectClassGradeByClassGradeId(classGradeId);
//		if(selectClassGradeByClassGradeId==null) {
//			json.put("status", 7);
//			json.put("message", "选择的班级不存在!");
//			return json;
//		}
		// 查询班级是否有老师了
//		ClassGrade selectOneClassGradeByClassGradeId = classGradeMapper.selectOneClassGradeByClassGradeId(classGradeId);
//		if(selectOneClassGradeByClassGradeId!=null) {
//			//join以后查询，这个班级没有教师，首先缔造关联
//			json.put("status", 8);
//			json.put("message", "该班级已有教师进行管理，请选择其他班级!");
//			return json;
//		}
		// 查询老师，查重
		Teacher queryOneTeacherByTno;
		try {
			queryOneTeacherByTno = teacherMapper.queryOneTeacherByTno(tno);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "服务器内部错误");
			return json;
		}
		// 如果没有存在记录说明第一次来注册：直接进入认领流程
		// 存在记录说明可能被拒绝或者等待中或者已同意的过来注册
		// 这里是已存在的：
		if (queryOneTeacherByTno != null) {
			if ("agree".equals(queryOneTeacherByTno.getRegisterStatus())) {
				json.put("status", 3);
				json.put("message", "教师账号注册失败，该老师已经存在!");
				return json;
			}
			// 注册时初始化状态waiting
			if ("waiting".equals(queryOneTeacherByTno.getRegisterStatus())) {
				json.put("status", 4);
				json.put("message", "该账号已经提交过注册申请，请等待管理员审核!");
				return json;
			}
			if ("refuse".equals(queryOneTeacherByTno.getRegisterStatus())) {
				Teacher teacher = new Teacher(tno, name, md5withkey, email, registerStatus);
				try {
					// 更新老师状态字段
					teacherMapper.updateTeacher(teacher);
					// 对班级表无人认领的老师进行认领
					// 查出需要改哪个班级实体类
//							ClassGrade queryOneClassGradeByClassGradeId = classGradeMapper
//									.queryOneClassGradeByClassGradeId(classGradeId);
				} catch (Exception e) {
					e.printStackTrace();
					json.put("status", -1);
					json.put("message", "服务器内部错误");
					return json;
				}
				json.put("status", 0);
				json.put("message", "再次注册成功，请等待管理员审核!");
				return json;
			}
			// 构造教师实体类(除了classGradeId以外，添加对应记录)
			Teacher teacher = new Teacher(tno, name, md5withkey, email, registerStatus);
			// 进行添加
			try {
				teacherMapper.addOneTeacher(teacher);
				// 对班级表无人认领的老师进行认领
				// 构造一个班级实体类
//				ClassGrade classGrade=new ClassGrade(classGradeId);
//				classGradeMapper.updateClassGrade(classGrade);
			} catch (Exception e) {
				e.printStackTrace();
				json.put("status", -1);
				json.put("message", "服务器内部错误");
				return json;
			}
			json.put("status", 0);
			json.put("message", "申请注册成功，请等待管理员审核!");
			return json;
		}
		// 到这里是没有记录的
		else {
			// 构造教师实体类(除了classGradeId以外，添加对应记录)
			Teacher teacher = new Teacher(tno, name, md5withkey, email, registerStatus);
			try {
				// 进行添加
				teacherMapper.addOneTeacher(teacher);
				// 对班级表无人认领的老师进行认领
				// 构造一个班级实体类
//					ClassGrade classGrade=new ClassGrade(classGradeId);
//					classGradeMapper.updateClassGrade(classGrade);
			} catch (Exception e) {
				e.printStackTrace();
				json.put("status", -1);
				json.put("message", "服务器内部错误");
				return json;
			}
			json.put("status", 0);
			json.put("message", "申请注册成功，请等待管理员审核!");
			return json;
		}
	}

	/**
	 * 
	 * @describe 教师修改个人信息
	 * @param tno   教师工号
	 * @param name  教师姓名
	 * @param phone 教师手机号
	 * @param email 教师邮箱
	 * @return
	 */
	@RequestMapping(value = "modifyTeacherInfo", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject modifyTeacherInfo(@RequestBody Map<String, String> map) {
		JSONObject json = new JSONObject();
		// 数据获取
		String tno;
		String sex;
		String name;
		String phone;
		String email;
		String idcard;
		// 验证电话格式
		try {
			tno = map.get("tno");
			sex = map.get("sex");
			name = map.get("name");
			phone = map.get("phone");
			email = map.get("email");
			idcard = map.get("idcard");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", 1);
			json.put("message", "传入数据不合法!");
			return json;
		}
		// 校验格式：
		// 校验手机号
		boolean checkphone = MailUtil.checkphone(phone);
		if (checkphone == false) {
			json.put("status", 5);
			json.put("message", "手机号格式不正确!");
			return json;
		}
		// 校验身份证号
		boolean idCard2 = PropertyCheckUtil.isIdCard(idcard);
		if (idCard2 == false) {
			json.put("status", 6);
			json.put("message", "身份证格式不正确!");
			return json;
		}
		// 校验姓名
		boolean name2 = PropertyCheckUtil.isName(name);
		if (name2 == false) {
			json.put("status", 7);
			json.put("message", "姓名格式不正确!");
			return json;
		}
		// 校验邮箱
		boolean checkemail = MailUtil.checkemail(email);
		if (checkemail == false) {
			json.put("status", 8);
			json.put("message", "邮箱格式不正确!");
			return json;
		}
		// 教师注册状态校验，只有agree状态的才能进行状态修改
		Teacher queryOneTeacherByTno;
		try {
			queryOneTeacherByTno = teacherMapper.queryOneTeacherByTno(tno);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "服务器内部错误");
			return json;
		}
		if (queryOneTeacherByTno == null) {
			json.put("status", 2);
			json.put("message", "操作失败，该教师不存在!");
			return json;
		}
		// 被拒绝了
		if ("refuse".equals(queryOneTeacherByTno.getRegisterStatus())) {
			json.put("status", 3);
			json.put("message", "该账号申请已经被管理员拒绝了，请重新进行申请后再次操作!");
			return json;
		}
		// 在等待状态
		if ("waiting".equals(queryOneTeacherByTno.getRegisterStatus())) {
			json.put("status", 4);
			json.put("message", "该账号申请正在审核中，管理员通过后方可进行操作!");
			return json;
		}
		// 构造教师实体
		Teacher teacher = new Teacher();
		teacher.setTno(tno);
		teacher.setSex(sex);
		teacher.setName(name);
		teacher.setEmail(email);
		teacher.setPhone(phone);
		teacher.setIdcard(idcard);
		try {
			// 进到这里说说明是agree状态允许修改
			teacherMapper.updateTeacher(teacher);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "服务器内部错误");
			return json;
		}
		json.put("status", 0);
		json.put("message", "操作成功!");
		return json;
	}

	/*
	 * 添加单个公司信息
	 */
	@PostMapping("addCompany")
	@ResponseBody
	public JSONObject inserCompany(@RequestBody Map<String, String> map) {
		JSONObject json = new JSONObject();
		int status = -1;
		String message = "添加失败，请重试";
		Company company = new Company();
		company.setCompanyId(UUIDUtil.getUUID());
		boolean result = false;
		String name = map.get("name");
		String phone = map.get("phone");
		String email = map.get("email");
		String intro = map.get("intro");
		String address = map.get("address");
		boolean checkphone = MailUtil.checkphone(phone);
		boolean checkemail = MailUtil.checkemail(email);
		if (!checkphone) {
			message = "号码格式错误";
			status = 1;
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		if (!checkemail) {
			message = "邮箱格式错误";
			status = 2;
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		if (name == null || phone == null || email == null || intro == null || address == null || name.equals("")
				|| phone.equals("") || intro.equals("") || email.equals("") || address.equals("")) {
			message = "信息需要填写完整";
			status = 3;
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		if (companyService.queryCompanyByCompanyName(name) != null) {
			status = 4;
			json.put("status", status);
			message = "已存在该公司";
			json.put("message", message);
			return json;
		}
		try {
			company.setPhone(phone);
			company.setName(name);
			company.setEmail(email);
			company.setIntro(intro);
			company.setAddress(address);
			result = companyService.insertCompany(company);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		if (result) {
			status = 0;
			message = "添加成功";
		}
		json.put("status", status);
		json.put("message", message);
		return json;
	}

	/*
	 * 批量删除通知
	 */
	@SuppressWarnings("unchecked")
	@PostMapping("/deleteInformList")
	@ResponseBody
	public JSONObject deleteInformByIdList(@RequestBody Map<String, Object> map) {
		JSONObject json = new JSONObject();

		Boolean result = false;

		String account = (String) map.get("account");
		List<String> list = (List<String>) map.get("list");
		result = informService.deleteInformByIdList(list, account);
		int status = -1;
		String message = "通知删除失败,请重试";
		if (result) {
			status = 200;
			message = "删除成功";
		}
		json.put("status", status);
		json.put("message", message);
		return json;
	}

	/*
	 * 发布通知
	 */
	@PostMapping("/insertInform")
	@ResponseBody
	public JSONObject insertInform(@RequestBody Map<String, String> map) {
		JSONObject json = new JSONObject();

		String account = map.get("account");
		String title = map.get("title");
		String content = map.get("content");

		int status = -1;
		String message = "通知发布失败,请重试";
		boolean result = false;
		result = informService.insertInform(title, content, account);
		if (result) {
			status = 200;
			message = "通知发布成功";
		}
		json.put("status", status);
		json.put("message", message);
		return json;
	}

	/**
	 * 发表评论
	 * 
	 * @param sno
	 * @param companyId
	 * @param content
	 * @return
	 */
	@RequestMapping(value = "comment/insertComment", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject insertComment(@RequestBody Map<String, String> map1) {
		int status = -1;
		JSONObject json = new JSONObject();
		String sno = map1.get("sno");
		String companyId = map1.get("companyId");
		String content = map1.get("content");
		String contentNotNUll = content.replaceAll(" ", "");
		try {
			Company company = commentService.findCompany(companyId);// 获取公司
			Student student = commentService.findStudent(sno);// 获取学生

			Comment comment = new Comment();
			comment.setCommentId(UUIDUtil.getUUID());
			comment.setCompany(company);
			comment.setStudent(student);
			comment.setCommitTime(DateUtil.getDate());
			comment.setContent(content);
			List<Comment> list = commentService.findCommentContentByStudentAndCompany(sno, companyId);
			Map<String, Object> map = new HashMap<>();
			for (Comment c : list) {
				if (c.getContent().equals(content)) {
					map.put("commentContent", c.getContent());
				}
			}
			if (contentNotNUll == null || contentNotNUll.equals("")) {
				json.put("status", 4);
				json.put("message", "评论内容不能都为空");
				return json;
			}
			if (content == null || content.equals("")) {
				json.put("status", 3);
				json.put("message", "评论内容为空");
			} else {
				if (map.get("commentContent") == null || map.get("commentContent").equals(null)) {
					boolean insertComment = commentService.insertComment(comment);
					if (insertComment) {
						status = 1;
						json.put("comment", comment);
						json.put("status", status);
						json.put("message", "评论添加成功");
					} else {
						status = 0;
						json.put("status", status);
						json.put("message", "评论添加失败");
					}
				}
				// TODO 可以但是没有必要
				else if (map.get("commentContent").equals(content)) {
					status = 2;
					json.put("status", status);
					json.put("message", "你已经对该公司提交过相同的内容了,请不要重复提交！");
				} else {
					boolean insertComment = commentService.insertComment(comment);
					if (insertComment) {
						status = 1;
						json.put("comment", comment);
						json.put("status", status);
						json.put("message", "评论添加成功");
					} else {
						status = 0;
						json.put("status", status);
						json.put("message", "评论添加失败");
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "评论添加失败");
			return json;
		}

		return json;
	}

	/**
	 * 删除评论
	 * 
	 * @param companyId
	 * @param sno
	 * @param content
	 * @return
	 */
	@RequestMapping(value = "comment/deleteComment", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject deleteComment(@RequestBody Map<String, String> map) {
		JSONObject json = new JSONObject();
		String commentId = map.get("commentId");
		String sno = map.get("sno");
		try {
			Comment comment = commentService.findCommentByCommentId(commentId);
			if (comment.getStudent().getSno() == sno || comment.getStudent().getSno().equals(sno)) {
				boolean deleteComment = commentService.deleteComment(comment);
				if (deleteComment) {
					json.put("status", 0);
					json.put("message", "删除评论成功！");
				} else {
					json.put("status", -1);
					json.put("message", "删除评论失败！");
				}
			} else {
				json.put("status", -2);
				json.put("message", "当前评论不是你提出的，不能进行删除");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "删除评论失败！");
			return json;
		}
		return json;
	}
	/*
	 * 公司图片上传
	 */
	@PostMapping("uploadCompanyImg")
	@ResponseBody
	public JSONObject uploadImg(MultipartFile file1,MultipartFile file2,MultipartFile file3,HttpServletRequest request){
		JSONObject json=new JSONObject();
		String companyId = request.getParameter("companyId");		
		String message="图片上传失败";
		MultipartFile[] file={file1,file2,file3};
		int status=companyService.upload(file, companyId, request);
		if(status==0){
			message="图片上传成功";
		}else if(status==1){
			message="只能上传jpg,png,gif格式的图片";
		}else if(status==2){
			message="文件为空";
		}
		json.put("status", status);
		json.put("message", message);
		return json;
	}
	
	/*
	 * 修改公司信息
	 */
	@PostMapping("/updateCompany")
	@ResponseBody
	public JSONObject updateCompany(@RequestBody Map<String, Object> map){
		JSONObject json=new JSONObject();
		
		List<String> img=(List<String>) map.get("img");
		System.out.println(img.toString());
		Company company=new Company();
		company.setCompanyId((String)map.get("companyId"));
		company.setName((String)map.get("name"));
		company.setAddress((String)map.get("address"));
		company.setIntro((String)map.get("intro"));
		company.setPhone((String)map.get("phone"));
		company.setEmail((String)map.get("email"));
		int status=-1;
		String message="修改失败，请重试";
		boolean result=companyService.updateCompany(company);
		if(result){
			status=200;
			message="修改成功";
		}
		json.put("status", status);
		json.put("message", message);
		return json;
	}
}
